<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="采购单号" prop="poNo">
        <el-input
          v-model="queryParams.poNo"
          placeholder="请输入采购单号"
          clearable
          oninput="value=value.replace(/[^\d\w]/g,'')" maxlength="20"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="付款类型" prop="paymentType">
        <el-select v-model="queryParams.paymentType" placeholder="请选择付款类型" clearable>
          <el-option
            v-for="dict in dict.type.erp_payment_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退货状态" prop="returnGoods">
        <el-select v-model="queryParams.returnGoods" placeholder="请选择退货状态" clearable>
          <el-option
            v-for="dict in dict.type.erp_return_goods"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="processType">
        <el-select v-model="queryParams.processType" placeholder="请选择审核类型" clearable>
          <el-option
            v-for="dict in dict.type.erp_purchase_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:purchase:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" >
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采购单号" align="center" prop="poNo" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="供应商联系人" align="center" prop="supplieruser" />
      <el-table-column label="供应商手机号" align="center" prop="supplierphone" />
      <el-table-column label="付款类型" align="center" prop="paymentType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_payment_type" :value="scope.row.paymentType"/>
        </template>
      </el-table-column>
      <el-table-column label="采购总价" align="center" prop="totalPurchase" />
<!--      <el-table-column label="入库类型" align="center" prop="warehousingType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_warehousing_type" :value="scope.row.warehousingType"/>
        </template>
      </el-table-column>-->
      <el-table-column label="类型" align="center" prop="processType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_purchase_type" :value="scope.row.processType"/>
        </template>
      </el-table-column>
      <el-table-column label="退货状态" align="center" prop="returnGoods">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_return_goods" :value="scope.row.returnGoods"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleInfo(scope.row)"
            v-hasPermi="['system:sale:remove']"
          >查看/审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            v-if="scope.row.processType === 0"
            @click="handleDeliver(scope.row)"
            v-hasPermi="['system:sale:remove']"
          >采购入库</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.processType === 1"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:purchase:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-if="scope.row.processType === 1"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:purchase:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="采购单号" prop="poNo">
          <el-input v-model="form.poNo" placeholder="请输入采购单号" />
        </el-form-item>
        <el-form-item label="供应商id" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入供应商id" />
        </el-form-item>
        <el-form-item label="付款类型" prop="paymentType">
          <el-select v-model="form.paymentType" placeholder="请选择付款类型">
            <el-option
              v-for="dict in dict.type.erp_payment_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库类型" prop="warehousingType">
          <el-select v-model="form.warehousingType" placeholder="请选择入库类型">
            <el-option
              v-for="dict in dict.type.erp_warehousing_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退货状态" prop="returnGoods">
          <el-select v-model="form.returnGoods" placeholder="请选择退货状态">
            <el-option
              v-for="dict in dict.type.erp_return_goods"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商联系人id" prop="supplieruser">
          <el-input v-model="form.supplieruser" placeholder="请输入供应商联系人id" />
        </el-form-item>
        <el-form-item label="审核类型" prop="processType">
          <el-select v-model="form.processType" placeholder="请选择审核类型">
            <el-option
              v-for="dict in dict.type.erp_process_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建人id" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入创建人id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPurchase, getPurchase, delPurchase, addPurchase, updatePurchase, editFlag} from "@/api/erp/purchase";

export default {
  name: "Purchase",
  dicts: ['erp_warehousing_type', 'erp_return_goods', 'erp_process_type', 'erp_payment_type','erp_purchase_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购订单表格数据
      purchaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        poNo: null,
        supplierId: null,
        paymentType: null,
        warehousingType: null,
        returnGoods: null,
        processType: null,
        createUser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购订单列表 */
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then(response => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        poNo: null,
        supplierId: null,
        paymentType: null,
        warehousingType: null,
        returnGoods: null,
        supplieruser: null,
        processType: null,
        createTime: null,
        updateTime: null,
        createUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path: '/addPurchase'});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({path: '/editPurchase',query: {id: row.id}});
    },
    /** 查看 */
    handleInfo(row) {
      this.$router.push({path: '/inFoPurchase',query: {id: row.id}});
    },
    /** 采购入库 */
    handleDeliver(row) {
      this.$router.push({path: '/deliverPurchase',query: {id: row.id}});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePurchase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除采购订单编号为"' + ids + '"的数据项？').then(function() {
        return delPurchase(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/purchase/export', {
        ...this.queryParams
      }, `purchase_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
